package com.java.springboot.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.java.springboot.Realm.UserRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class configShiro {

    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

    /**
     * 配置安全管理器
     * @return
     */
    @Bean
    public DefaultWebSecurityManager SecurityManager(){
        DefaultWebSecurityManager SecurityManager = new DefaultWebSecurityManager();
        SecurityManager.setRealm(userRealm());
        return SecurityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(SecurityManager());
        shiroFilterFactoryBean.setLoginUrl("/login");
        Map<String,String> map = new LinkedHashMap<>();
        map.put("/css/**","anon");
        map.put("/js/**","anon");
        map.put("/login","anon");
        map.put("/index","anon");
        map.put("/**","user");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }

    /**
     * 配置shiro权限标签
     * @return
     */
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }

}
